<?php
class AlbumsForm extends BaseForm {
	private $search;
	private $albums;
	private $selected;
	
	public function AlbumsForm(){
		parent::BaseForm();
		
		$this->albums = array();
		$this->selected = array();
	}
	
	public function clear(){
		$this->albums = array();
		$this->selected = array();
		
		$this->search = null;
	}
	
	public function getAlbums(){ return $this->albums; }
	public function getSearch(){ return $this->search; }
	public function getSelected(){ return $this->selected; }
	
	public function setAlbums($val){ $this->albums = $val; }
	public function setSearch($val){ $this->search = $val; }
	public function setSelected($val){$this->selected = $val; }
	
	public function updateAlbum($id, $name){
		$album = $this->findAlbum($id);
		
		if($album != null){
			$album->setName($name);
		}
	}
	
	public function addAlbum($id, $name, $userId, $seqNo){
		$album = new Album(); 
		$album->setId($id);
		$album->setName($name);
		$album->setUserId($userId);
		$album->setSeqNo($seqNo);
		
		$this->albums[] = $album; 
	}
	
	public function findAlbum($id){
		$album = null;
		
		foreach($this->getAlbums() as $current){
			if($id == $current->getId()){
				$album = $current; 
				
				break;
			}
		}
		return $album; 
	}
	
	public function removeAlbums($selected){
		$flip = array_flip($selected);
		$arr = array();
		
		foreach($this->getAlbums() as $current){
			$id = $current->getId();
			
			if(! array_key_exists($id, $flip)){
				$arr[] = $current;
			}
		}
		
		// the added will replace the existent ones
		$this->setAlbums($arr);
	}
	
	public function getSelectedAlbums($selected){
		$flip = array_flip($selected);
		$arr = array(); 
		
		foreach($this->getAlbums() as $current){
			$id = $current->getId();
			
			if(array_key_exists($id, $flip)){
				$arr[] = $current;
			}
		}
		
		return $arr;
	}
	
	public function deleteAlbums($ids){
		$arr = array();
		$flip = array_flip($ids);
		
		foreach($this->getAlbums() as $current){
			$id = $current->getId();
			
			if(! array_key_exists($id, $flip)){
				$arr[] = $current;
			}
		}
		
		$this->setAlbums($arr);
	}
	
	public function updateAlbumDefaultPicture($albumId, $id){
		// find the album and set the new picture id
		foreach($this->albums as $current){
			if($albumId == $current->getId()){
				$current->setPictureId($id);
				
				break;
			}
		}
	}
	
} 
?>